package com.gateway.controller;

import com.gateway.dao.GatewayMapper;
import com.gateway.entity.GatewayEntity;
import com.gateway.service.GatewayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/4/28
 * 网关配置类
 * 将初始化配置放在启动类中自动获取
 * GatewayConfigApplicationRunner
 **/
@RestController
@RequestMapping("/getGateWayConfig")
public class GatewayController {

    @Autowired
    private GatewayService gatewayService;
    @Autowired
    private GatewayMapper gatewayMapper;

    /**
     * 初始化默认网关配置
     * @return
     */
    @GetMapping("/initAllRoute")
    public String saveGatewayConfig(){
        initAllGatewayRouteConfig();
        return "正在配置中....";
    }

    @GetMapping("/synRouteConfig")
    public String synRouteConfig(GatewayEntity gatewayEntity){
        Integer result = gatewayMapper.updateGatewayConfig(gatewayEntity);
        initAllGatewayRouteConfig();
        return result > 0 ? "数据更新成功,正在同步中..." : "同步失败";
    }

    /**
     * 获取数据库网关配置
     */
    public void initAllGatewayRouteConfig(){
        gatewayService.loadAllRoutes();
    }

}
